package com.tiyende.credit.loop.ui.dialog

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonOnItemChildClickListener
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.lib.common.shapeview.layout.ShapeLinearLayout
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonDialog
import com.tiyende.credit.loop.databinding.TiyendeDialogAccountSubmitCompleteTipsBinding
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.utils.TiyendeDensityUtil
import com.tiyende.credit.loop.utils.TiyendeImageUtil
import com.tiyende.credit.loop.utils.decoration.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/21 13:45
 * @Annotation：绑卡确认dialog
 */
class TiyendeAccountCompleteDialog: CommonDialog {
    private lateinit var binding: TiyendeDialogAccountSubmitCompleteTipsBinding
    private var mCallback: IDialogCallback? = null

    private var itemAdapter: ItemAdapter? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogAccountSubmitCompleteTipsBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
        initWalletListView()
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.walletSubmit)
        setOnClickListener(binding.imageClose)//关闭
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        mCallback?.onClick(v)
    }

    /**
     * 初始化账户列表
     */
    private fun initWalletListView() {
        val topDecoration = TiyendeDensityUtil.dp2px(12f)
        binding.walletListView.addItemDecoration(LinearSpacingItemDecoration(0, 0, topDecoration, topDecoration))
        binding.walletListView.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.walletListView)
        binding.walletListView.adapter = itemAdapter
        binding.walletListView.itemAnimator = DefaultItemAnimator()
        itemAdapter?.setOnItemChildClickListener(CommonOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter?.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.tiyende_authentication_step_account_item) {
        override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.walletTitle,model.optString("walletDesc"))
            helper.setText(R.id.walletMobile,"+" + model.optString("walletMobile"))
            val walletLogo = helper.getImageView(R.id.walletLogo)
            TiyendeImageUtil.instance?.loadImageUrl(mContext,walletLogo,model.optString("walletLogo"), R.mipmap.tiyende_holder)

            val itemLayout = helper.getView<ShapeLinearLayout>(R.id.itemLayout)
            itemLayout.shapeDrawableBuilder.setSolidColor(Color.parseColor("#000000")).intoBackground()

            helper.setVisibility(R.id.walletDelete,View.INVISIBLE)
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS)
    }
}